﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.IO;
using System.Xml;
using AliCMS;
using System.Text.RegularExpressions;

namespace AliCMS.CourseModule.Bll
{
	public class Category : DbObjectBase, ICategory
	{
		public Category(int instanceID, string lang, bool checkRight, string database)
			: base(instanceID, lang, checkRight, database)
		{
		}

		public override object ExecuteService(HttpContext context)
		{
			object result = null;

			string operation = context.Request.QueryString["Operation"];
			if (operation == "Add")
			{
				Dictionary<string, object> item = new Dictionary<string, object>();
				string value = null;
				value = context.Request.Form["Name"];
				if (value != null) item[Alias.Category.Name + Lang] = value;
				value = context.Request.Form["ParentID"];
				if (value != null) item[Alias.Category.ParentID] = value;
				value = context.Request.Form["Picture"];
				if (value != null) item[Alias.Category.Picture] = value;
				value = context.Request.Form["Recommend"];
				if (value != null) item[Alias.Category.Recommend] = value;
				value = context.Request.Form["IsDisplay"];
				if (value != null) item[Alias.Category.IsDisplay] = value;
				value = context.Request.Form["Sort"];
				if (value != null) item[Alias.Category.Sort] = value;

				result = Add(item);
			}
			else if (operation == "Modify")
			{
				int id = Convert.ToInt32(context.Request.QueryString["ID"]);

				Dictionary<string, object> changes = new Dictionary<string, object>();
				string value = null;
				value = context.Request.Form["Name"];
				if (value != null) changes[Alias.Category.Name + Lang] = value;
				value = context.Request.Form["ParentID"];
				if (value != null) changes[Alias.Category.ParentID] = value;
				value = context.Request.Form["Picture"];
				if (value != null) changes[Alias.Category.Picture] = value;
				value = context.Request.Form["Recommend"];
				if (value != null) changes[Alias.Category.Recommend] = value;
				value = context.Request.Form["IsDisplay"];
				if (value != null) changes[Alias.Category.IsDisplay] = value;
				value = context.Request.Form["Sort"];
				if (value != null) changes[Alias.Category.Sort] = value;

				Modify(id, changes);
			}
			else if (operation == "Delete")
			{
				int id = Convert.ToInt32(context.Request.QueryString["ID"]);

				Delete(id);
			}
			else if (operation == "Display")
			{
				string id = context.Request.QueryString["ID"];

				string[] ids = id.Split('|');
				for (int i = 0; i < ids.Length; i++)
				{
					if (String.IsNullOrEmpty(ids[i]))
						continue;

					Display(Convert.ToInt32(ids[i]));
				}
			}
			else if (operation == "UnDisplay")
			{
				string id = context.Request.QueryString["ID"];

				string[] ids = id.Split('|');
				for (int i = 0; i < ids.Length; i++)
				{
					if (String.IsNullOrEmpty(ids[i]))
						continue;

					UnDisplay(Convert.ToInt32(ids[i]));
				}
			}
			else if (operation == "Recommend")
			{
				string id = context.Request.QueryString["ID"];

				string[] ids = id.Split('|');
				for (int i = 0; i < ids.Length; i++)
				{
					if (String.IsNullOrEmpty(ids[i]))
						continue;

					Recommend(Convert.ToInt32(ids[i]));
				}
			}
			else if (operation == "UnRecommend")
			{
				string id = context.Request.QueryString["ID"];

				string[] ids = id.Split('|');
				for (int i = 0; i < ids.Length; i++)
				{
					if (String.IsNullOrEmpty(ids[i]))
						continue;

					UnRecommend(Convert.ToInt32(ids[i]));
				}
			}
			else if (operation == "Move")
			{
				int from = Convert.ToInt32(context.Request.QueryString["From"]);
				int to = Convert.ToInt32(context.Request.QueryString["To"]);

				Move(from, to);
			}
			else if (operation == "MoveRecommend")
			{
				int from = Convert.ToInt32(context.Request.QueryString["From"]);
				int to = Convert.ToInt32(context.Request.QueryString["To"]);

				MoveRecommend(from, to);
			}
			else if (operation == "List")
			{
				string value = null;

				int page = -1;
				value = context.Request.QueryString["Page"];
				if (value != null) page = Convert.ToInt32(value);

				int pageSize = -1;
				value = context.Request.QueryString["PageSize"];
				if (value != null) pageSize = Convert.ToInt32(value);

				string condition = context.Request.QueryString["Condition"];
				string sort = context.Request.QueryString["Sort"];

				result = List(page, pageSize, condition, sort);
			}
			else if (operation == "GetInnerIDFromGuid")
			{
				Guid guid = new Guid(context.Request.QueryString["ID"]);
				result = GetInnerIDFromGuid(guid);
			}
			else if (operation == "Get")
			{
				object id = null;
				if (Regex.IsMatch(context.Request.QueryString["ID"], @"^\d+$"))
					id = Convert.ToInt32(context.Request.QueryString["ID"]);
				else
					id = new Guid(context.Request.QueryString["ID"]);
				result = Get(id);
			}
			else
			{
				throw new Exception("lang_header_param");
			}

			return result;
		}

		public int GetInnerIDFromGuid(Guid guid)
		{
			return Dal.GetInnerIDFromGuid(guid);
		}
		public Guid GetGuidFromInnerID(object id)
		{
			return Dal.GetGuidFromInnerID(id);
		}
		public Dictionary<string, object> Get(object id)
		{
			if (CheckRight)
			{
				Guid guid;
				if (id is Guid)
					guid = (Guid)id;
				else
					guid = Dal.GetGuidFromInnerID(id);
				ValidateRight("Get", guid);
			}

			return Dal.Get(id);
		}
		public Guid Add(Dictionary<string, object> item)
		{
			if (CheckRight)
			{
				ValidateRight("Add", Guid.Empty);
			}

			if (!item.ContainsKey(Alias.Category.ID) || item[Alias.Category.ID].ToString() == String.Empty)
				item[Alias.Category.ID] = Guid.NewGuid();
			else if (!(item[Alias.Category.ID] is Guid))
				item[Alias.Category.ID] = new Guid(item[Alias.Category.ID].ToString());

			if (item.ContainsKey(Alias.Category.ParentID))
			{
				string parentID = item[Alias.Category.ParentID].ToString();
				if (parentID == String.Empty || parentID == Guid.Empty.ToString())
					item[Alias.Category.ParentID] = DBNull.Value;
				else
					item[Alias.Category.ParentID] = new Guid(parentID);
			}

			Dal.Add(item);

			return (Guid)item[Alias.Category.ID];
		}
		public void Modify(int id, Dictionary<string, object> changes)
		{
			if (CheckRight)
			{
				Guid guid = Dal.GetGuidFromInnerID(id);
				ValidateRight("Modify", guid);
			}

			if (changes.ContainsKey(Alias.Category.ParentID))
			{
				string parentID = changes[Alias.Category.ParentID].ToString();
				if (parentID == String.Empty || parentID == Guid.Empty.ToString())
					changes[Alias.Category.ParentID] = DBNull.Value;
				else
					changes[Alias.Category.ParentID] = new Guid(parentID);
			}

			Dal.Modify(id, changes);
		}
		public void Delete(int id)
		{
			if (CheckRight)
			{
				Guid guid = Dal.GetGuidFromInnerID(id);
				ValidateRight("Delete", guid);
			}

			#region 删除代表图
			Dictionary<string, object> item = Get(id);

			string picture = item[Alias.Category.Picture].ToString();
			if (picture.StartsWith("/") && File.Exists(Manager.MapPath(picture)))
				File.Delete(Manager.MapPath(picture));
			#endregion

			Dal.Delete(id);
		}
		public void Display(int id)
		{
			if (CheckRight)
			{
				Guid guid = Dal.GetGuidFromInnerID(id);
				ValidateRight("Display", guid);
			}

			Dictionary<string, object> changes = new Dictionary<string, object>();
			changes[Alias.Category.IsDisplay] = true;

			Dal.Modify(id, changes);
		}
		public void UnDisplay(int id)
		{
			if (CheckRight)
			{
				Guid guid = Dal.GetGuidFromInnerID(id);
				ValidateRight("UnDisplay", guid);
			}

			Dictionary<string, object> changes = new Dictionary<string, object>();
			changes[Alias.Category.IsDisplay] = false;

			Dal.Modify(id, changes);
		}
		public void Recommend(int id)
		{
			if (CheckRight)
			{
				Guid guid = Dal.GetGuidFromInnerID(id);
				ValidateRight("Recommend", guid);
			}

			Dictionary<string, object> changes = new Dictionary<string, object>();
			changes[Alias.Category.IsRecommended] = true;

			Dal.Modify(id, changes);
		}
		public void UnRecommend(int id)
		{
			if (CheckRight)
			{
				Guid guid = Dal.GetGuidFromInnerID(id);
				ValidateRight("UnRecommend", guid);
			}

			Dictionary<string, object> changes = new Dictionary<string, object>();
			changes[Alias.Category.IsRecommended] = false;

			Dal.Modify(id, changes);
		}
		public void Move(int from, int to)
		{
			if (CheckRight)
			{
				ValidateRight("Move", Guid.Empty);
			}

			Dal.Move(from, to);
		}
		public void MoveRecommend(int from, int to)
		{
			if (CheckRight)
			{
				ValidateRight("MoveRecommend", Guid.Empty);
			}

			Dal.MoveRecommend(from, to);
		}
		public DataTable List(int page, int pageSize, string condition, string sort)
		{
			if (CheckRight)
			{
				ValidateRight("List", Guid.Empty);
			}

			return Dal.List(page, pageSize, condition, sort);
		}
		public int GetRecordCount(string condition)
		{
			return Dal.GetRecordCount(condition);
		}

		#region Properties
		private Dal.ICategory Dal
		{
			get { return (Dal.ICategory)DalBase; }
		}
		#endregion
	}
}
